package com.qingxin.park.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.park.entity.MaterialType;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;


/**
 * 物资分类
 * @author Administrator
 *
 */
@Service
public class MaterialTypeService extends BaseService<MaterialType, String> {
	
	
	/**
	 * 获取菜单树数据
	 * @param parkid
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TreeNode> getUITreeData(String pid, boolean isParent){ 
		List<TreeNode> list = new ArrayList<TreeNode>();
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.park.entity.MaterialType(p.typeid,p.typeCode,p.typeName,p.typeKind)");
		hql.append(" from MaterialType p ");
		hql.append(" order by p.typeName asc");
		List<MaterialType> types = listByHql(hql.toString());
		if(types != null){
			for(MaterialType t : types){   
				list.add(new TreeNode(t.getTypeid(), t.getTypeName(), "TYPE", null, isParent));
			}
		}
		return list;
	}
	
	
	
	@SuppressWarnings("unchecked")
	public List<MaterialType> listByTypeKind(Integer typeKind){
		List<MaterialType> list = listByHql("from MaterialType where typeKind=?", typeKind);
		return list;
	}
	
	
	
	/**
	 * 获类别列表分页数据
	 * @param parkName
	 * @param parkStatus
	 * @param pageParam
	 * @param loginUser
	 * @return
	 
	 */
	
	public Pager list(PageParam pageParam,String search,String leftdate, LoginUser loginUser,HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.park.entity.MaterialType(" +
				"typeid,typeCode,typeName,typeKind)" +
				" from MaterialType");
		if(StringUtils.isNotEmpty(search)){
			search = "%"+search.replace("'", "")+"%";
			querier.append(" and (typeName like :typeName)");
			querier.setParam("typeName", search);
		}
		querier.append(pageParam.getOrderby(" order by typeName desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	/**
	 * 删除类别信息 
	 * @param typeId
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError delete(String[] typeid, LoginUser loginUser, HttpServletRequest request){
		for(String id : typeid){
			MaterialType materialType = get(id);
			if(materialType==null){
				continue;           
			}
			Object obj = getByHql("select materialid from Material where materialType.typeid=?", id);
			if(obj!=null){
				return new SysError("类别 “"+materialType.getTypeName()+"”下有物资信息，不能删除.");
			}
			this.delete(materialType);
		}
		return null;
	}
	
	/**
	 * 添加类别
	 * @param category
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(MaterialType materialType,LoginUser loginUser, HttpServletRequest request){
		materialType.setMaterials(null);
		this.save(materialType);
		return null;
	}
	
	/** 
	 * 修改类别
	 * @param materialType
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(MaterialType materialType, LoginUser loginUser, HttpServletRequest request){
		MaterialType old = getSafeEntity(materialType.getTypeid());
		String fields = "typeCode,typeName,typeKind";
		BeanUtil.setTargetFields(old, materialType, fields, true);
		Updater<MaterialType> updater = new Updater<>(old, fields);
		this.update(updater);
		return null;
	}
	

	/**
	 * 判断指定名称是否被使用
	 * @param typeName
	 * @param typekid
	 * @param 
	 * @return
	 */
	public boolean isNameDuplicate(String typeName, String typeid){
		if(StringUtils.isEmpty(typeid)){
			Object obj = getByHql("select typeid from MaterialType where typeName=?", typeName);
			return obj!=null;
		}else {
			Object obj = getByHql("select typeid from MaterialType where typeid=? and typeName=?", typeid, typeName);
			return obj!=null;
		}
	}
	
	/**
	 * 判断指定CODE是否被使用
	 * @param typeCode
	 * @param typeid
	 * @return
	 */
	public boolean isCodeDuplicate(String typeCode, String typeid){
		if(StringUtils.isEmpty(typeid)){
			Object obj = getByHql("select typeid from MaterialType where typeCode=?", typeCode);
			return obj!=null;
		}else {
			Object obj = getByHql("select typeid from MaterialType where typeid=? and typeCode=?", typeid, typeCode);
			return obj!=null;
		}
	}
	
}
